package com.asap.demo.mysql.orm;


import com.asap.demo.mysql.util.Constants;
import com.asap.demo.mysql.util.FuncStatic;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Map;

public final class JDBCUtils {
	private static final Logger logger = LoggerFactory.getLogger(JDBCUtils.class);

	private static ComboPooledDataSource c3p0DataSource = null;
	private static  JDBCUtils instance;

	private JDBCUtils(Map<String,String> params) {
		initialC3p0Pool(params);
	}

	public static ComboPooledDataSource getC3p0DataSource() { return c3p0DataSource; }

	public static void setC3p0DataSource(ComboPooledDataSource c3p0DataSource) { JDBCUtils.c3p0DataSource = c3p0DataSource; }


	private void initialC3p0Pool(Map<String,String> params) {
		try {
			c3p0DataSource = new ComboPooledDataSource();
			c3p0DataSource.setUser(params.get(Constants.DB_JDBC_USER));
			String password=params.get(Constants.DB_JDBC_SECRET);
			if(password.startsWith("ENC(")){
				String algorithm=params.get("jasypt.algorithm");
				String salt=params.get("jasypt.encryptor.password");
				password=password.substring(4, password.length()-1);
				password= FuncStatic.jasyptDecode(algorithm, salt, password);
			}
			c3p0DataSource.setPassword(password);
			c3p0DataSource.setJdbcUrl(params.get(Constants.DB_JDBC_URL));
			c3p0DataSource.setDriverClass(params.get(Constants.DB_JDBC_DRIVER));
			c3p0DataSource.setInitialPoolSize(Integer.valueOf(params.get(Constants.INITAL_POOL_SIZE)));
			c3p0DataSource.setMinPoolSize(Integer.valueOf(params.get(Constants.MIN_POOL_SIZE)));
			c3p0DataSource.setMaxPoolSize(Integer.valueOf(params.get(Constants.MAX_POOL_SIZE)));
			c3p0DataSource.setMaxStatements(Integer.valueOf(params.get(Constants.MAX_STATE_ELEMENTS)));
			c3p0DataSource.setMaxIdleTime(Integer.valueOf(params.get(Constants.MAX_IDLE_TIME)));
		} catch (PropertyVetoException e) {
			logger.error("初始化C3P0数据库连接池异常！",e);
		}
	}

	public static JDBCUtils getInstance(Map<String,String> params) {
		if(instance == null) {
			synchronized(JDBCUtils.class) {
				instance = new JDBCUtils(params);
			}
		}
		return instance;
	}


	public Connection getC3P0Connection() {
		Connection conn = null;
		if (null != c3p0DataSource) {
			try {
				conn = c3p0DataSource.getConnection();
			} catch (SQLException e) {
				logger.error("getC3P0Connection",e);
			}
		}
		return conn;
	}


	public void beginTransaction(Connection conn) {
		if(conn != null) {
			try {
				conn.setAutoCommit(false);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				logger.error("beginTransaction",e);
			}
		}
	}
	

	 
	public void commitTx(Connection conn) {
		if(conn != null) {
			try {
				conn.commit();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				logger.error("commitTx",e);
			}
		}
	}
	

	public void rollBackBusiness(Connection conn) {
		if(conn != null) {
			try {
				conn.rollback();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				logger.error("rollBackBusiness",e);
			}
		}
	}

	public void closeConnect(Connection conn) {
		if(conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				logger.error("close connect error",e);
			}
		}
	}
	
}	
